/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package argtrust.util;

import java.util.LinkedHashMap;
import java.util.ArrayList;
import java.util.List;

//import aima.core.logic.fol.parsing.ast.KEY;
/**
 *
 * @author Yuqing Tang
 */
public class ListMultiValueCache<KEY, VALUE> {

    private LinkedHashMap<KEY, List<VALUE>> mHashTable;

    public ListMultiValueCache() {
        mHashTable = new LinkedHashMap<KEY, List<VALUE>>();
    }

    public VALUE getValue(KEY key) {
        List<VALUE> val_list = mHashTable.get(key);
        if (val_list == null) {
            return null;
        } else {
            return val_list.get(0);
        }
    }

    public List<VALUE> getValueList(KEY key) {
        List<VALUE> val_list = mHashTable.get(key);
        if (val_list == null) {
            return null;
        } else {
            return val_list;
        }
    }

    public void put(KEY key, VALUE value) {
        List<VALUE> val_list = getValueList(key);
        if (val_list == null) {
            val_list = new ArrayList<VALUE>();
            val_list.add(value);
            mHashTable.put(key, val_list);
        } else {
            int val_index = val_list.indexOf(value);
            if (val_index == -1) {
                val_list.add(value);
            }
        }
    }

    public VALUE getNextValue(KEY key, VALUE value)  {
        List<VALUE> val_list = getValueList(key);
        if (val_list == null) {
            return null;
        } else {
            int val_index = val_list.indexOf(value);
            if (val_index == -1) {
//                throw new edu.cuny.argtrust.basics.ArgTrustException("No original value in getNextValue");
                //The original value is not in the cache; Error; TODO: should throw an error.
                return null;
            } else if (val_index < val_list.size() - 1) {
                return val_list.get(val_index + 1);
            } else {
                return null;
            }
        }
//        return null;
    }
}
